package day_2022_4_to_7.day48;

/**
 * 顺时针打印矩阵
 * @author haomin
 * @date 2022/07/10 09:11
 **/
public class Day48_2_PrintArr {
    public static void main(String[] args) {
        int[][] arr = {{57,50,59,18,31,13},{67,86,93,86,4,9},{38,98,83,56,82,90},{66,50,67,11,7,69},{20,58,55,24,66,10},{43,26,65,0,64,28},{62,86,38,19,37,98}};
        clockwisePrint(arr,7,6);
    }
    public static int[] clockwisePrint(int[][] mat, int n, int m) {
        int[] ret = new int[n * m];
        int index = 0;
        int topX = 0, topY = 0, lowX = n - 1, lowY = m - 1;
        while (topX <= lowX && topY <= lowY) {
            if (topX == lowX) {    //只剩下一行时
                for (int i = topY; i <= lowY; i++)
                    ret[index++] = mat[topX][i];
                return ret;
            }
            if (topY == lowY) {  //只剩下一列时
                for (int i = topX; i <= lowX; i++)
                    ret[index++] = mat[i][topY];
                return ret;
            }
//            for (int i = topX; i < lowX; i++)
//                ret[index++] = mat[topY][i];
//            for (int i = topY; i < lowY; i++)
//                ret[index++] = mat[i][lowX];
//            for (int i = lowX; i > topX; i--)
//                ret[index++] = mat[lowY][i];
//            for (int i = lowY; i > topY; i--)
//                ret[index++] = mat[i][topX];
            for (int i = topY; i < lowY; i++)
                ret[index++] = mat[topX][i];
            for (int i = topX; i < lowX; i++)
                ret[index++] = mat[i][lowY];
            for (int i = lowY; i > topY; i--)
                ret[index++] = mat[lowX][i];
            for (int i = lowX; i > topX; i--)
                ret[index++] = mat[i][topY];

            topX++;
            topY++;
            lowX--;
            lowY--;
        }
        return ret;
    }
}